//
//  planeButton.m
//  打飞机
//
//  Created by wangwei on 16/9/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "planeButton.h"
#define SCREENSIZE [UIScreen mainScreen].bounds.size

@implementation planeButton

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.userInteractionEnabled = YES;
    
    
    
}


-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.userInteractionEnabled = YES;
    }
    
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    self.highlighted = YES;
    
    
    
}



-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = touches.anyObject;
    
    CGPoint nowLoc = [touch locationInView:touch.view];
    
    CGPoint preLoc = [touch previousLocationInView:touch.view];
    
    
    CGFloat offsetX = nowLoc.x - preLoc.x;
    CGFloat offsetY = nowLoc.y - preLoc.y;
    
    if(self.frame.origin.x + offsetX > 0 && self.frame.origin.x+self.bounds.size.width + offsetX< SCREENSIZE.width && self.frame.origin.y+offsetY > 0 && self.frame.origin.y + self.bounds.size.height + offsetY < SCREENSIZE.height){
    self.center = CGPointMake(self.center.x+offsetX, self.center.y+offsetY);
    }
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.highlighted = NO;
}


@end
